﻿
var queryListUrl = ArrangeUrl + "/Doctor/getTpDoctorList";
var newArrangeUrl = ArrangeUrl + "/scheduling/save";
var mqueryDeptListUrl = OrgUrl + "/dept/getDeptList";
//当前选择模板
var _nowselect_doctor = "";
var _doctorIds = "";

var _selectBeginTm = "";
var _selectEndTm = "";

$(function () {

    loadDoctorTableData();
    //取消选择模板
    $("#but_cancel").click(function () {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    });

    //确定选择模板
    $("#but_ok").click(function () {
        chooseDoctor();
    });

});


//选择模板
function chooseDoctor() {

    var _chkVal = "";
    $("#t_table tr").each(function () {
        var _chkbox = $(this).find("td:eq(0)").find("input[type='checkbox']");
        if (_chkbox.prop("checked")) {
            _chkVal += $(this).find("td:eq(1)").html() + "\2";
        }
    });

    if (_chkVal != "" && _chkVal != undefined) {
        var index = parent.layer.getFrameIndex(window.name);
        parent.loadOneTpDoctor(_chkVal);
        parent.layer.close(index);
    } else {
        layer.alert("请选择一个医生");
    }


}

function queryInfo() {
    loadDoctorTableData();
}

//初始化表格数据
function loadDoctorTableData() {
    var _html = "";

    var _deptId =GetQueryString("dept");
    var valuestr = unescape(getCookie(_deptId));

    //打开页面初始化查询的开始时间，结束时间.
/*    _selectBeginTm = valuestr.split("\2")[0];
    _selectEndTm = valuestr.split("\2")[1];*/
    //过滤医生id
    _doctorIds = valuestr.split("\2")[2];
    var _doctorIdArr = [];

    if (_doctorIds != "" && _doctorIds != undefined) {

        var _idsArr = _doctorIds.split("\5");
        for (var i = 0; i < _idsArr.length; i++) {
            _doctorIdArr.push(_idsArr[i]);
        }
    }

    $.ajax({
        url: queryListUrl,
        type: 'get',
        data: {
            doctorName: $("#text_search").val(),
            deptId: GetQueryString("dept"),
            isSpeciallyInvite: "",
        }, beforeSend: function () {
            layer.msg('加载中...', { icon: 16 });
        },
        complete: function () {
            layer.closeAll();
        },
        success: function (d) {

            if (JSON.parse(d).status == "0") {
                var _json = JSON.parse(d).data;
                var doctorList = _json;
                for (var i = 0; i < doctorList.length; i++) {
                    if (_doctorIdArr.indexOf(doctorList[i].doctorId) == -1) {
                        var _doctorId = doctorList[i].doctorId;
                        var _doctorKey = doctorList[i].doctorKey;
                        var _doctorName = doctorList[i].doctorName;
                        var _deptId = doctorList[i].deptId;
                        var _doctorTitle = doctorList[i].doctorTitle;
                        var _isSpeciallyInvite = doctorList[i].isSpeciallyInvite == "1" ? "是" : "否";
                        var _checkBox = "<input type='checkbox' id='' name='' onclick='change_selectstyle(this)'/>";
                        _html += "<tr id='" + JSON.stringify(doctorList[i]) + "'>" +
                                     "<td>" + _checkBox + "</td>" +
                                     "<td style='display:none'>" + _doctorId + "</td>" +
                                     "<td>" + _doctorKey + "</td>" +
                                     "<td>" + _doctorName + "</td>" +
                                     "<td>" + _isSpeciallyInvite + "</td>" +
                                +"</tr>";
                    }
                }
                $("#doctor_table").html(_html);
            }
        }
    });
}

function CheckAllDoctor(t) {

    var _isChecked = $(t).prop("checked");
    if (_isChecked) {
        $("#t_table tr").each(function () {

            var _chkbox = $(this).find("td:eq(0)").find("input[type='checkbox']");
            _chkbox.prop("checked", _isChecked);

            $(this).css({
                "background": "#1AACFF",
                "color": "white"
            });
        });
    } else {
        $("#t_table tr").each(function () {

            var _chkbox = $(this).find("td:eq(0)").find("input[type='checkbox']");
            _chkbox.prop("checked", _isChecked);

            $(this).css({
                "background": "white",
                "color": "black"
            });
        });
    }

}

//切换选中样式
function change_selectstyle(t) {

    _isChecked = $(t).prop("checked");

    if (_isChecked) {
        $(t).parent().parent().css({
            "background": "#1AACFF",
            "color": "white"
        })
    } else {
        $(t).parent().parent().css({
            "background": "white",
            "color": "black"
        });
    }

}
